﻿using System;
using System.Collections.Generic;
using System.Web;
using He.Web.UI;
using System.Web.SessionState;
using System.Text.RegularExpressions;
using He.Common;
using System.Text;

namespace He.Web.tools
{
    /// <summary>
    /// collect_ajax 的摘要说明
    /// </summary>
    public class collect_ajax : AjaxHandler
    {

        public override void ProcessRequest(HttpContext context)
        {
            //检查是否登录后上传操作
            if (!new He.Web.UI.Manage().IsAdminLogin())
            {
                context.Response.Write("{\"msg\": \"0\", \"msbox\": \"请登录后再进行上传文件！\"}");
                return;
            }
            InputUtil input = new InputUtil();
            string url = InputUtil.Form("url");
            //采集html源
             string html = RemoteHelper.GetRemoteHtml(url, Encoding.GetEncoding("gb2312"));
            //获取标题
            string title = "";
            string content = "";
            foreach (string reg in titleRegArr)
            {
                title = RemoteHelper.GetRegValue(reg, html);
                if (!string.IsNullOrEmpty(title))
                {
                    break;
                }
            }
            //去除多余标签及字符
            title = RemoteHelper.RegDropSides(title, "<.*?>", "</.*?>");
            title = title.Replace("-", "").Replace("—", "").Replace("中新网", "").Trim();
            //将半角双引号转换为全角双引号
            foreach (Match m in Regex.Matches(title, "\"(.*?)\""))
            {
                string cut_title = m.Value.Replace("\"", "");
                title = title.Replace(m.Value, "“" + cut_title + "”");
            }
            //获取内容
            foreach (string reg in contentRegArr)
            {
                content = RemoteHelper.GetRegValue(reg, html);
                if (!string.IsNullOrEmpty(content))
                {
                    break;
                }
            }
            content = RemoteHelper.RemoveConTarget(content);
            if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(content))
            {
                EchoJson(0, "<font color='red'>× </font>" + title + url + "<font color='red'> 采集失败</font>");
                return;
            }
            //采集入库
            int category_id = InputUtil.Form("category_id", 0);
            He.BLL.article bll = new BLL.article();
            He.Model.article model = new Model.article();
            //判断文章来源是否已存在
            if (bll.ExistsFrom(url))
            {
                EchoJson(1, "<font color='green'>! </font>" + title + url + "<font color='green'> 文章已存在，退出采集。</font>");
                return;
            }
            model.title = title;
            model.seo_keywords = title;
            model.seo_desc = He.Common.StringHelper.ClearHtmlTags(content, 250);
            model.content = content;
            model.category_id = category_id;
            model.form = url;
            model.add_time = DateTime.Now.ToString();
            bll.Add(model);
            EchoJson(1, "<font color='green'>√ </font>" + title + url + "<font color='green'> 采集成功</font>");
        }

        #region 详细页正则匹配集合
        public string[] titleRegArr = { "<title>(.*?)</title>" };
        public string[] contentRegArr = { 
                                            "<!--正文start-->([\\w\\W]*?)<!--正文start-->", 
                                            "<!--图片说明-->([\\w\\W]*?)<!--图片说明-->", 
                                            "<!--正文-->([\\w\\W]*?)<!--正文-->",                                           
                                            "<div(.*?)id=\"ad0\"(.*?)>([\\w\\W]*?)</div>" ,
                                            "<div id=\"content\"(.*?)>([\\w\\W]*?)</div>",
                                            "<font id=\"Zoom\"(.*?)>([\\w\\W]*?)</font>"
                                        };
        #endregion

        public override bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}